/* mutil.c
**
** Copyright (c) 2007, Meadhbh S. Hamrick
** All rights reserved.
** 
** Redistribution  and  use  in  source  and  binary  forms,  with  or  without
** modification, are permitted provided that the following conditions are met:
** 
**  * Redistributions of  source code must  retain the above  copyright notice,
**    this list of conditions and the following disclaimer.
** 
**  * Redistributions in binary form must reproduce the above copyright notice,
**    this list of conditions and the following disclaimer in the documentation
**    and/or other materials provided with the distribution.
** 
**  * Neither the  name of  Project Meadhbh  nor the names of  its contributors
**    may be used  to endorse  or promote  products derived from this  software
**    without specific prior written permission.
** 
** THIS SOFTWARE IS PROVIDED BY  THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
** AND ANY  EXPRESS OR IMPLIED WARRANTIES,  INCLUDING, BUT NOT  LIMITED TO, THE
** IMPLIED WARRANTIES  OF MERCHANTABILITY AND FITNESS FOR  A PARTICULAR PURPOSE
** ARE DISCLAIMED.  IN NO  EVENT SHALL THE  COPYRIGHT OWNER OR  CONTRIBUTORS BE
** LIABLE  FOR  ANY  DIRECT,   INDIRECT,  INCIDENTAL,  SPECIAL,  EXEMPLARY,  OR
** CONSEQUENTIAL  DAMAGES  (INCLUDING,  BUT  NOT  LIMITED  TO,  PROCUREMENT  OF
** SUBSTITUTE GOODS  OR SERVICES;  LOSS OF USE,  DATA, OR PROFITS;  OR BUSINESS
** INTERRUPTION)  HOWEVER CAUSED  AND ON  ANY THEORY  OF LIABILITY,  WHETHER IN
** CONTRACT,  STRICT LIABILITY,  OR  TORT (INCLUDING  NEGLIGENCE OR  OTHERWISE)
** ARISING IN ANY WAY  OUT OF THE USE OF THIS SOFTWARE,  EVEN IF ADVISED OF THE
** POSSIBILITY OF SUCH DAMAGE.
** 
** $Id: mutil.c 69 2008-06-24 20:02:18Z meadhbh.siobhan $
*/

/** \file mutil.c
 ** This file contians the definition of a number of useful utility functions.
 */

/** \mainpage mutil - Meadhbh Utilities
 **
 ** here's some introductory text.
 */

/* Macro Definitions */
#define MUTIL_KERNEL

/* File Includes */
#include "mutil.h"

/* Typedefs, Structs, Unione, Enums, etc. */

/* Static Function Prototypes */

/* Static Variable Declarations */

static char *mu_local_sourceid = "$Id: mutil.c 69 2008-06-24 20:02:18Z meadhbh.siobhan $";

/* Function Definitions */

/** \fn int mu_cpp_find( char *array[], char *trial, unsigned int size)
**  \brief Find the index of an a string in an array of strings
**  \param array an array of character pointers
**  \param trial pointer to the string to find
**  \param size size of the array pointed to by array
**  \return the index of the trial string in the array
*/

int mu_cpp_find( char *array[], char *trial, unsigned int size ) {
  int  rv,i;

  rv = -1;

  do {
    if( NULL == array ) break;
    if( NULL == trial ) break;

    for( i = 0; i < size; i++ ) {
      if( NULL == array[i] ) break;

      if( !strcmp( trial, array[i] ) ) {
	rv = i;
	break;
      }
    }
    
  } while( 0 );

  return( rv );
}

/** \fn int mu_cp_hexprint( int fd, char *array, unsigned int size)
**  \brief output the contents of memory in a human readable form
**  \param fd file descriptor to output to
**  \param array pointer to the beginning of the memory block to output
**  \param size length of the memory block to output
*/
void mu_cp_hexprint( int fd, char *array, unsigned int size ) {  
}

/** \fn int mu_cp_fhexprint( FILE *fp, char *array, unsigned int size)
**  \brief output the contents of memory in a human readable form
**  \param fp FILE descriptor to output to
**  \param array pointer to the beginning of the memory block to output
**  \param size length of the memory block to output
*/
void mu_cp_fhexprint( FILE *fp, char *array, unsigned int size ) {
  unsigned int i;
  char buffer[17];

  buffer[16] = (char) 0;

  for( i = 0; i < size; i++ ) {
    if( 0 == ( i % 16 ) ) {
      memset( buffer, 32, 16 );
      fprintf(fp, "%08X:", i);
    }
    
    fprintf( fp, " %02X", ( (unsigned int) array[i] ) & 0xFF );
    if( ( array[i] < 32 ) || ( array[i] > 126 ) ) {
      buffer[ i % 16 ] = '.';
    } else {
      buffer[ i % 16 ] = array[i];
    }

    if( 15 == ( i % 16 ) ) {
      fprintf( fp, " %16.16s\n", buffer );
    }
  }

  if( ( i % 16 ) > 0 ) {
    for( ; ( i % 16 ) > 0 ; i++ ) {
      fprintf(fp, "   " );
    }
    fprintf( fp, " %16.16s\n", buffer );    
  }
}
